﻿using System;
using System.Drawing;
using System.Windows.Forms;
using SuperMap.Data;
using SuperMap.Mapping;
using SuperMap.UI;

namespace MapTimeDataPlayTest
{
    public partial class Form1 : Form
    {
        private Workspace workspace;
        private MapControl mapControl;
        private Timer timer;
        private int totalTick;

        public Form1()
        {
            InitializeComponent();

            workspace = new Workspace();
            mapControl = new MapControl();
            mapControl.Map.Workspace = workspace;
            mapControl.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top;
            mapControl.Size = new Size(600, 500);
            this.Controls.Add(mapControl);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo();
            connectionInfo.Type = WorkspaceType.SMWU;
            connectionInfo.Server = "..\\..\\..\\data\\covid-19_TEST.smwu";
            connectionInfo.Name = "Test";

            workspace.Open(connectionInfo);

            mapControl.Map.Open("confirmed_cases_daily@convit-19");

            Map map = mapControl.Map;
            Layer layer = map.Layers["confirmed_cases_daily@convit-19#1"];

            layer.TimeStartField = "hizuke";
            layer.TimeEndField = "hizuke";
            layer.TimeStepInterval = new TimeSpan(1, 0, 0, 0);
            layer.EnableTimeFilter = true;

            map.TimeStep = new TimeSpan(1, 0, 0, 0);
            map.TimeWindow = new TimeSpan(0, 0, 0, 0);
            map.CurrentPlayerTick = 0;

            map.TimeTextPosition = TextPosition.BottomRight;
            map.IsShowTime = true;

            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += TimerOnTick;
        }

        private void TimerOnTick(object sender, EventArgs e)
        {
            mapControl.Map.CurrentPlayerTick += 1;
            if (mapControl.Map.CurrentPlayerTick >= totalTick)
            {
                timer.Stop();
            }
            mapControl.Map.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mapControl.Map.TimeEnable = true;
            mapControl.Map.MapStartTime = mapControl.Map.GetDefaultMapStartTime();
            mapControl.Map.MapEndTime = mapControl.Map.GetDefaultMapEndTime();
            totalTick = (int)((mapControl.Map.MapEndTime -  mapControl.Map.MapStartTime).Ticks / mapControl.Map.TimeStep.Ticks);
            timer.Start();
        }
    }
}
